package com.example.boot1.conttoller;

import com.example.boot1.annoself.MethodExporter;
import com.example.boot1.bean.*;
import com.example.boot1.service.VueUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;



@RestController
public class VueUserController {

    @Autowired
    private VueUserService vueUserService;

    @MethodExporter //自定义注解测试
    @PostMapping("/vue/login")
    @ResponseBody
    public ResponseEntity<VueResponseToken> login(@RequestBody VueAccountBean vueAccountBean){
        System.out.println("login-用户账号:"+vueAccountBean.getAccount());
        System.out.println("login-用户密码:"+vueAccountBean.getPassword());
        ResponseEntity responseEntity = null;
        VueResponseToken vueResponseToken = new VueResponseToken();
        try {
            Thread.sleep(1500);
        }catch (Exception e){
            e.printStackTrace();
        }

        if ("222".equals(vueAccountBean.getAccount())){
            vueResponseToken.setCode("200");
            vueResponseToken.setMsg("登录成功");
            vueResponseToken.setToken("cfl15829936517");
            responseEntity = ResponseEntity.status(HttpStatus.OK).body(vueResponseToken);
        }else{
            vueResponseToken.setCode("202");
            vueResponseToken.setMsg("用户名或密码不正确");
            vueResponseToken.setToken(null);
            responseEntity = ResponseEntity.status(HttpStatus.ACCEPTED).body(vueResponseToken);
        }
        return responseEntity;
    }

    //ResponseEntity 返回修改http 请求状态
    @PostMapping("/vue/register")
    @ResponseBody
    public ResponseEntity<VueDataBean> reg(@RequestBody VueAccountBean vueAccountBean){
        System.out.println("reg-用户账号:"+vueAccountBean.getAccount());
        System.out.println("reg-用户密码:"+vueAccountBean.getPassword());
        ResponseEntity responseEntity = null;
        VueDataBean vueDataBean = new VueDataBean();
        if ("111".equals(vueAccountBean.getAccount())){
            vueDataBean.setCode(200);
            vueDataBean.setMsg("注册成功！");
            responseEntity = ResponseEntity.status(HttpStatus.OK).body(vueDataBean);
        }else{
            vueDataBean.setCode(202);
            vueDataBean.setMsg("账户已注册或账户密码不符合要！");
            responseEntity = ResponseEntity.status(HttpStatus.ACCEPTED).body(vueDataBean);
        }
        return responseEntity;
    }


    @GetMapping("/vue/userList")
    @ResponseBody
    public ResponseEntity<BackPageDataBean> userList(HttpServletRequest request,Integer page){
        System.out.println("分页接受当前页: "+page);


        String token = request.getHeader("Authorization");
        System.out.println("当前token: "+token);

        ResponseEntity responseEntity = null;

        BackPageDataBean backPageDataBean = null;

        BackDataBean backDataBean = null;

        //System.out.println("接受前端传过来的token值："+token);
        //进行数据库操作
        int pageOffSetNum = page * 10;
        List ulist = vueUserService.findUserPage(pageOffSetNum);

        //token
        if ("cfl15829936517".equals(token)){
            if (null!= ulist &&  ulist.size()>0){
                backDataBean = new BackDataBean();
                backDataBean.setCode(200);
                backDataBean.setMsg("查询成功！");
                backDataBean.setResult(ulist);

                backPageDataBean = new BackPageDataBean(backDataBean);
                backPageDataBean.setPage(page);
                backPageDataBean.setTotal(vueUserService.findAllTotal());
                responseEntity = ResponseEntity.status(HttpStatus.OK).body(backPageDataBean);
            }else{
                backDataBean = new BackDataBean();
                backDataBean.setCode(2001);
                backDataBean.setMsg("没有数据！");
                backDataBean.setResult(null);

                backPageDataBean = new BackPageDataBean(backDataBean);
                responseEntity = ResponseEntity.status(HttpStatus.OK).body(backPageDataBean);
            }
        }else{
            backDataBean = new BackDataBean();
            backDataBean.setCode(202);
            backDataBean.setMsg("没有权限！");
            backDataBean.setResult(null);
            backPageDataBean = new BackPageDataBean(backDataBean);
            responseEntity = ResponseEntity.status(HttpStatus.ACCEPTED).body(backPageDataBean);
        }
        return responseEntity;
    }
    @ResponseBody
    @GetMapping("/vue/insertBig")
    public String insertBig(){
        System.out.println("执行插入前系统当前时间=>"+System.currentTimeMillis());
        vueUserService.insertBig();
        System.out.println("执行插入后系统当前时间=>"+System.currentTimeMillis());
        return "wangcheng";
    }

}
